package com.qk.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.entity.Dept;
import com.qk.mapper.DeptMapper;
import com.qk.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class DeptServiceImpl implements DeptService {
    @Autowired
    private DeptMapper deptMapper;


    @Override
    public void add(Dept dept) {
        //补全创建时间
        dept.setCreateTime(LocalDateTime.now());
        dept.setUpdateTime(LocalDateTime.now());
        //调用mapper
        deptMapper.insert(dept);
    }

    @Override
    public PageResult <Dept> list (String name, Integer status, Integer page, Integer pageSize) {
        //设置Page Helpper分页参数
        PageHelper.startPage(page, pageSize);
        //调用mapper接口，执行查询
        List <Dept> deptList = deptMapper.selectList(name, status);
        //解析并分装分页结果
        Page<Dept>p=(Page<Dept>) deptList;
        return new PageResult<>(p.getTotal(),p.getResult());
    }

    @Override
    public Dept getByID(Integer id) {
        return deptMapper.selectByID(id);
    }

    @Override
    public void upDate(Dept dept) {
        //补全更新时间
        dept.setUpdateTime(LocalDateTime.now());
        deptMapper.upDate(dept);
    }

    @Override
    public void deletById(Integer id) {
        deptMapper.deletById(id);
    }

    @Override
    public Result  allList(Dept dept) {
        List<Dept>deptList=deptMapper.selectAllList();
        return Result.success(deptList);
    }
}
